<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>

<jsp:useBean id="nowDate" class="java.util.Date" />
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>管理编辑</title>
	<!-- import css and js --> 
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var basic_Url="${ctx}/yd/logic/sowarefreeze";
var grid;
$(function(){
	//表格结构
	var griddata ={};
	grid = $("#table1").ligerGrid({
        columns: [{ display: 'ID', name: 'id', width: 1 , hide:true},
                  { display: '产品id', name: 'materialsid', width: 1 , hide:true},//使用selecturl时必须设置该属性
                  { display: '<font color="gray">产品编码</font>', name: 'prodNo', width: 100},
				  { display: '<font color="gray">产品名称</font>', name: 'prodName', width: 90 },
				  { display: '<font color="gray">型号规格</font>', name: 'prodSpecDesc', width: 150 },
				  { display: '<font color="gray">是否可用批次</font>', name: 'openBatch', hide:true,width: 160, render: function (item){
						if(item.openBatch=="Y"){
							return "可用";
						}else{
							return "不可用";
						}
				  }},
				  { display: '<font color="gray">产品等级</font>', name: 'status', width: 70 ,
					  render: function (item){
            		  var state=item.status;
            		  if(state=="1"){
            			  return "一等品";
            		  }
            		  if(state=="2"){
            			  return "次品";
            		  }
            		  if(state=="3"){
            			  return "旧件";
            		  }
            	  	}
				  },
				  { display: '<font color="gray">基本单位</font>', name: 'prodBUnit', width: 80},
				  { display: '<font color="gray">单位换算率</font>', name: 'prodUnitConvert', width: 70},
				  { display: '<font color="gray">单位</font>', name: 'prodUnit', width: 70 },
				  
				  {display : '<font color="gray">本次可冻结数量</font>',name : 'tmpCanFrozenQuantity',width : 100
						,render:function(item){
							item.tmpCanFrozenQuantity =
								item.prodQuantity - item.ckQuantity - item.frozenQuantity;
							return item.tmpCanFrozenQuantity+"";
						  }
				  },
                  {display : '申请冻结数量<font color="red">*</font>',name : 'sqdjQuantity',width : 80
                	  //<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus }">
                	  ,editor:{type:"float"},
      				render:function(item){
      					if(!isNaN(item.sqdjQuantity) && !isNaN(item.prodUnitConvert)){
      			    		item.sqdjBQuantity = item.sqdjQuantity*item.prodUnitConvert ;
      			    	}else{
      			    		item.sqdjBQuantity=null;//清除值
      			    	}
      				  return item.sqdjQuantity;
      			  	}
                	//</c:if>
      			  },{display : '<font color="gray">申请冻结基本单位数量</font>',name : 'sqdjBQuantity',width : 150},
      			  { display: '实际冻结基本单位数量', name: 'sjdjBQuantity', width: 1 , hide:true,
      				render:function(item){
      					if(undefined == item.sjdjBQuantity || isNaN(item.sjdjBQuantity) ||0 == item.sjdjBQuantity ){
      			    		item.sjdjBQuantity = "0" ;
      			    	}
      				  return item.sjdjBQuantity;
      				}
      			  },
                  { display: '<font color="gray">本单实际冻结数量</font>', name: 'sjdjQuantity', width: 100,
      				render:function(item){
      					if(undefined == item.sjdjQuantity || isNaN(item.sjdjQuantity) ||0 == item.sjdjQuantity ){
      			    		item.sjdjQuantity = "0" ;
      			    	}
      				  return item.sjdjQuantity;
      				}
      			  },
      			  { display: '冻结出库数量', name: 'sjdjCkBQty', width: 1 , hide:true,
        				render:function(item){
        					if(undefined == item.sjdjCkBQty ||null == item.sjdjCkBQty || isNaN(item.sjdjCkBQty)  ){
        			    		item.sjdjCkBQty = "0" ;
        			    	}
        				  return item.sjdjCkBQty+"";
        				}
        		  },
        		  { display: '<font color="gray">本单冻结出库数量</font>', name: 'sjdjCkQty', width: 100,
        			  render:function(item){
      					if(undefined == item.sjdjCkQty ||null == item.sjdjCkQty || isNaN(item.sjdjCkQty)  ){
      			    		item.sjdjCkQty = "0" ;
      			    	}
      				  return item.sjdjCkQty+"";
      				}
      		      },
      		      { display: '冻结解冻数量', name: 'sjdjJdBQty', width: 1 , hide:true,
        				render:function(item){
        					if(undefined == item.sjdjJdBQty ||null == item.sjdjJdBQty || isNaN(item.sjdjQuantity)){
        			    		item.sjdjJdBQty = "0" ;
        			    	}
        				  return item.sjdjJdBQty+"";
        				}
        		  },
      		      { display: '<font color="gray">本单冻结解冻数量</font>', name: 'sjdjJdQty', width: 100,
      				render:function(item){
      					if(undefined == item.sjdjJdQty ||null == item.sjdjJdQty || isNaN(item.sjdjJdQty) ){
      			    		item.sjdjJdQty = "0" ;
      			    	}
      				  return item.sjdjJdQty+"";
      				}
      		       },
      		     { display: '<font color="gray">订单基本单位数量</font>', name: 'prodBQuantity', width: 1 , hide:true},
				  { display: '<font color="gray">订单数量</font>', name: 'prodQuantity', width: 80},
                 { display: '<font color="gray">已出库基本单位数量</font>', name: 'ckBQuantity', width: 1 , hide:true ,
					  render:function(item){
						  if(isNaN(item.ckBQuantity)||0==item.ckBQuantity){
							  item.ckBQuantity='0';
						  }
						  if(!isNaN(item.prodUnitConvert)){
	      			    		item.ckQuantity = (item.ckBQuantity/item.prodUnitConvert).toFixed(4) ;
	      			    	}else{
	      			    		item.ckQuantity=null;//清除值
	      			    	}
						  return item.ckBQuantity;
					  }
				  },
				  { display: '<font color="gray">已出库数量</font>', name: 'ckQuantity', width: 80/* , type: 'float' */},
                  { display: '<font color="gray">订单已冻结基本单位数量</font>', name: 'frozenBQuantity', width: 1 , hide:true},
                  { display: '<font color="gray">订单已冻结数量</font>', name: 'frozenQuantity', width: 90},
                  { display: '备注', name: 'remark', width: 150
      		    	//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus }">
      		    	   ,editor: { type: 'text'}
      		    	//</c:if>
      		       }
                  ],
                  enabledEdit: true,
                  usePager: true,pageSize:1000,pageSizeOptions:[1000],
                  //data : griddata,
                   parms:[{ name : 'id' , value : '${soware.id}'}],
                  url : basic_Url+"/griditems.do",
                  width: "100%", height:'90%',
                  checkbox : true,
                //<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus }">
      			  toolbar : {
      				items : [
      				    {line : true}, 
      				    {	text : '删除',
      						click : deleteRow,
      						img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
      					} 
      				]
      			  }
				//</c:if>
     });
	
	//表格数据显示
	/* var manager = $("#table1").ligerGetGridManager();
	//<c:forEach items='${sowareitem}' var='item'>
    	manager.addRow2({
    		id : "${item.id}" ,
    		materialsid:"${item.materialsid}",
    		prodNo :"${item.prodNo}",
    		prodName: "${item.prodName}",
    		prodSpecDesc :"${item.prodSpecDesc}",
    		prodBUnit :"${item.prodBUnit}",
    		prodBQuantity :"${item.prodBQuantity}",
    		prodUnit :"${item.prodUnit}",
    		status :"${item.status}",
    		prodUnitConvert:"${item.prodUnitConvert}",
    		prodQuantity :"${item.prodQuantity}",
    		ckBQuantity :"${item.ckBQuantity}",
    		ckQuantity :"${item.ckQuantity}",
    		frozenBQuantity :"${item.frozenBQuantity}",
    		frozenQuantity :"${item.frozenQuantity}",
    		sqdjBQuantity :"${item.sqdjBQuantity}",
    		sqdjQuantity :"${item.sqdjQuantity}",
    		sjdjBQuantity :"${item.sjdjBQuantity}",
    		sjdjQuantity :"${item.sjdjQuantity}",
    		sjdjCkQty:"${item.sjdjCkQty}",
    		sjdjCkBQty:"${item.sjdjCkBQty}",
    		sjdjJdQty:"${item.sjdjJdQty}",
    		sjdjJdBQty:"${item.sjdjJdBQty}",
    		openBatch :"${item.openBatch}",
    		remark :"${item.remark}"
        });
    //</c:forEach> */
	
	//表单验证处理
	$("#inputForm").validate({			   
	onsubmit: true,
	 /*执行ajaxsubmit  */ 
    submitHandler: function(editform) {
        try{			    		
            url =  basic_Url+"/save.do";
            var api = frameElement.api;
        	var W = api.opener;
            getReturnMessage(url,"inputForm",W.searchAP);
        }catch(e){alert(e);}
    }
	});
	
	
	//添加模式隐藏DIV
	if("${flag}" =="ADD"){
		$("#tr1").hide();
		//$("#tr2").hide();
	}
	
	//按钮处理
	/* btn status */
	$("[id^='btn_fb_']").hide();
	var curStatus = "${soware.billstatus}";//当前所在环节、状态
	if(curStatus == ''){//建单
		$("#btn_fb_save").show();
	}else {
		//$("#btn_fb_exportExcel").show();
		formComponentDisabled(true);//禁用控件
		if (curStatus == '0'||curStatus == '3'){//草稿状态-提交二级
			if("${user.name}"=="${soware.dcruser}"){
				$("#btn_fb_approve_2").show();
				$("#btn_fb_cancel").show();
			}
			formComponentDisabled(false);//启用控件
		}else if (curStatus == '1'){//二级审批中状态
			$("#btn_fb_rebut_2").show();
			$("#btn_fb_end_2").show();
		}else if (curStatus == '4'){//取消状态
		}else{
		}
	}
	
	
	//页面加载标识隐藏
	$("#pageloading").hide();
	
	//=============================
    
    	//销售订单的值改变触发 //XXX 后台获取
    	$("#soNo").change(function(){ 
    		//<c:forEach items='${solist}' var='state'>
        	if($("#soNo").find("option:selected").text()=='${state.no}'){
        		$("#cliOrderNo").val('${state.cliOrderNo}');
        		$("#cliName").val('${state.cliName}');
        		$("#cliNo").val('${state.cliNo}');
        		$("#cliAddr").val('${state.cliAddr}');
        		$("#cliPeo").val('${state.cliPeo}');
        		$("#cliTel").val('${state.cliTel}');
        		$("#cliFax").val('${state.cliFax}');
        		$("#receipDate").val('<fmt:formatDate value="${state.receipDate}" pattern="yyyy-MM-dd" type="both" />');
        		$("#payMode").val('${state.payMode}');
        		$("#deliveryMode").val('${state.deliveryMode}');
        		$("#currency").val('${state.currency}');
        		$("#exchangeRate").val('${state.exchangeRate}');
        		$("#deliveryDate").val('<fmt:formatDate value="${state.deliveryDate}" pattern="yyyy-MM-dd" type="both" />');
        	}
       		//</c:forEach>
       		//根据订单编号获得订单的产品item
    		multipleSearch("#table1",true,"","${ctx}/yd/logic/soware/soitemlist.do?soNo="+$("#soNo").val());
    	}); 
    
});

	
	//表单提交前的处理
	function submitForm(param) {
		//设置ajax全局同步
		$("#paramflag").val(param);

		var data = grid.getData();
		var dl = grid.getDeleted();
		if (data.length == 0) {
			$.dialog.alert("请选择申请产品");
			return false;
		}
		
		var ba = true;
		
		if(data !=null){
			var j=0;
			$(data).each(function (){
				j++;
				//表格必填项
				
				if (undefined == this.sqdjQuantity
						|| isNaN(this.sqdjQuantity)
						|| this.sqdjQuantity <= 0) {
					$.dialog.alert("第" + j + "行的“申请冻结数量”数据不合法");
					ba = false;
					return false;
				}
				
				if(/* (this.prodQuantity - this.ckQuantity - this.frozenQuantity) */
						this.tmpCanFrozenQuantity < this.sqdjQuantity){
					$.dialog.alert("您第" + j + "的申请冻结数量大于可冻结数量");
					ba = false;
					return false;
				}


			});
		}
		
		jQuery.ajaxSettings.async = false;
		if (ba) {
			$("#itemdata").val(JSON.stringify(data));
			$("#itemdelete").val(JSON.stringify(dl));
			$("#submitFlag").val(param);
			formComponentDisabled(false);//启用控件
			$("#inputForm").submit();
		}
	}

	function tableSubmit(msg,param){
		$.ligerDialog.confirm(msg, 
				function (flag) { 
				if(flag){
					submitForm(param);
					}
			}); 
	}
	
	function submitFormClose(param){
		tableSubmit('是否确认关闭单据操作？',param);
	}
	
	function submitFormEnd(param){
		tableSubmit('是否确认审批完结操作？',param);
	}
	
	function submitFormRebut(param){
		tableSubmit('是否确认驳回操作？',param);
	}
	
	
	function deleteRow() {
		grid.deleteSelectedRow();
	}
	
	function exportExcel() {
		var path = basic_Url + "/exportExcel.do?id=${soware.id}";
		window.open(path, "_blank");
	}
	
	
    
</script>

</head>

<body>

<form id="inputForm" action="" method="post">
<input type="hidden" id="id" name="id" value="${soware.id}"/>
<input type="hidden" id="version" name="version" value="${soware.version}"/>
<input type="hidden" id="itemdata" name="itemdata" value=""/>
<input type="hidden" id="dataItem" name="dataItem"/><!-- 单位获取辅助 -->
<input type="hidden" id="itemdelete" name="itemdelete" value=""/>
<input type="hidden" id="paramflag" name="paramflag" value=""/>
<!-- 用于标识提交按钮 -->
<input type="hidden" id="submitFlag" value="" />
 <div class="pageContent" >
    <div class="panel">
         <table class="TableBlock" align="center">
         			<tr id="tr1">
   		           		<td nowrap="nowrap">单据编号:</td>
   		                <td>
   		                	<input type="text" id="no" name="no" value="${soware.no}" class="inp_width_98p required" readonly="readonly"/>
   		                </td>
   		                <td nowrap="nowrap" width="20%">建单时间:</td>
						<td width="30%"><input type="text" id="dctime" name="dctime"
							value="<fmt:formatDate value="${empty soware.dctime?nowDate:soware.dctime}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/></td>
   		           </tr>
                   <tr>
                    	<td nowrap="nowrap">销售订单编号:</td>
						<td>
						<c:choose>
						<c:when test="${'ADD' eq flag}">
						<select id="soNo" name="soNo" class="inp_width_98p required">
							<option> </option>
							<c:forEach items="${solist}" var="sodo">
								<option value="${sodo.no}">${sodo.no}</option>
							</c:forEach>
						</select>
						<script type="text/javascript">
   		                	$("#soNo").val("${soware.soNo}");
   		                </script>
						</c:when>
						<c:otherwise>
						<input type="text" value="${soware.soNo}" id="soNo" name="soNo" class="inp_width_98p required" readonly="readonly"></input>
						</c:otherwise>
						</c:choose>
   		            	</td>
                    	<td nowrap="nowrap"></td>
   		                <td></td>
   		           </tr>
   		           <tr>
   		           		
   		                <td nowrap="nowrap">客户编号:</td>
   		           		<td><input type="text" id="cliNo" name="cliNo" value="${soware.cliNo}" class="inp_width_98p required" readonly="readonly"></input></td>
   		           		<td nowrap="nowrap">客户单位名称:</td>
   		           		<td><input class="inp_width_98p" type="text" id="cliName" name="cliName" value="${soware.cliName}" class="inp_width_98p required" readonly="readonly"></input></td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">客户联系人:</td>
   		           		<td><input type="text" class="inp_width_98p" id="cliPeo" name="cliPeo" value="${soware.cliPeo}" readonly="readonly"></input></td>
   		           		<td nowrap="nowrap">客户联系电话:</td>
   		           		<td><input type="text" class="inp_width_98p" id="cliTel" name="cliTel" value="${soware.cliTel}" readonly="readonly"></input></td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">传真:</td>
   		           		<td><input type="text" class="inp_width_98p" id="cliFax" name="cliFax" value="${soware.cliFax}" readonly="readonly"></input></td>
   		           		<td nowrap="nowrap">交货时间:</td>
   		           		<td><input type="text" class="inp_width_98p" id="deliveryDate" name="deliveryDate" value="<fmt:formatDate value="${soware.deliveryDate}" pattern="yyyy-MM-dd" type="both" />"  readonly="readonly"></input></td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">客户地址:</td>
   		            	<td colspan="3">
   		                	<input class="inp_width_98p" type="text" id="cliAddr" name="cliAddr" value="${soware.cliAddr}" readonly="readonly"/>
   		             	</td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">付款方式:</td>
   		           		<td>
						<input type="text" value="${soware.payMode}" class="inp_width_98p" id="payMode" name="payMode" readonly="readonly"></input>
   		           		</td>
   		           		<td nowrap="nowrap">交货方式:</td>
   		           		<td>
						<input type="text" class="inp_width_98p" value="${soware.deliveryMode}" id="deliveryMode" name="deliveryMode" readonly="readonly"></input>
   		           		</td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">币别:</td>
   		           		<td>
						<input type="text" class="inp_width_98p" value="${soware.currency}" id="currency" name="currency" readonly="readonly"></input>
						</td>
   		           		<td nowrap="nowrap">汇率:</td>
   		           		<td><input type="text" class="inp_width_98p"  id="exchangeRate" name="exchangeRate" value="${soware.exchangeRate}" readonly="readonly"></input></td>
   		           </tr>
   		           <tr>
   		           		<td nowrap="nowrap">制单人:</td>
   		           		<td><input type="text" class="inp_width_98p" id="listerName" name="listerName" value="${empty soware.listerName?user.realName:soware.listerName}" readonly="readonly"></input></td>
   		           		<td nowrap="nowrap">制单人部门:</td>
   		           		<td>
   		           		<c:choose>
   		           		<c:when test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus }">
   		           		<select id="listerDeptName" name="listerDeptName" class="inp_width_98p">
								<option></option>
								<c:forEach items="${depts }" var="item">
									<option value="${item.depname }">${item.depname}</option>
								</c:forEach>
						</select> 
   		           			<script type="text/javascript">
   		           				$("#listerDeptName").val("${soware.listerDeptName}");
   		           		</script>
   		           		</c:when>
						<c:otherwise>
						<input type="text" class="inp_width_98p" value="${soware.listerDeptName}" id="listerDeptName" name="listerDeptName" readonly="readonly"></input>
						</c:otherwise>
						</c:choose>
   		           		</td>
   		           </tr>
   		           <%-- <tr id="tr2">
   		           		<td nowrap="nowrap">审核人:</td>
   		           		<td><input type="text" id="auditorName" name="auditorName" value="${soware.auditorName}" readonly="readonly"></input></td>
   		           		<td nowrap="nowrap">批准人:</td>
   		           		<td><input type="text" id="approverName" name="approverName" value="${soware.approverName}" readonly="readonly"></input></td>
   		           </tr> --%>
   		           <tr>
                    <td nowrap="nowrap">备注:</td>
   		                 <td colspan="3">
   		                <input class="inp_width_98p" type="text" id="remark" name="remark" value="${soware.remark}" />
   		                 </td>
   		           </tr>
                </table>
                <div id="table1"> </div>               
         </div>
         <div class="formBar">
	        <ul>
		        <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="btn_fb_save" onclick="submitForm(0)">保存</button></div></div></li>
		        <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="btn_fb_approve_2" onclick="submitForm(1)" title="提交二级审核">提交</button></div></div></li>
		      	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="btn_fb_cancel" onclick="submitFormClose(4)" title="取消订单">取消</button></div></div></li>
		      	
		      	<webres:displaycontroller elementId="LOGIC_SOWAREFREEZE_2WANJIE" description="销售冻结申请二级完结">
		      	<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="btn_fb_end_2" onclick="submitFormEnd(2)" title="二级结案">审核</button></div></div></li>
		      	</webres:displaycontroller>
		      	<webres:displaycontroller elementId="LOGIC_SOWAREFREEZE_2BOHUI" description="销售冻结申请二级驳回">
		      	<li><div class="button"><div class="buttonContent"><button type="button" id="btn_fb_rebut_2" onclick="submitFormRebut(3)" title="二级驳回到草稿">驳回</button></div></div></li>
		      	</webres:displaycontroller>
		      	
		      	<li><div class="button"><div class="buttonContent"><button type="button" id="btn_fb_exportExcel" class="export" onclick="exportExcel()">导出</button></div></div> </li>
		       <li><div class="button"><div class="buttonContent"><button type="button" onclick="CloseDialog()">关闭</button></div></div></li>
	        </ul>
        </div>    
    </div>
</form>
</body>
</html>
